﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;
using Kugar.Core.Security.PermissionTarget;

namespace Kugar.UI.WinForm.Security.Providers
{
    [Export(typeof(IUI_ControlPermissionProcessProvider))]
    [ExportMetadata("ProviderName", "5B80BFA3-691F-495E-83B1-AA0B7B44DFB6")]
    [Browsable(false)]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class UI_DefaultControlPermissionProcessProvider : IUI_ControlPermissionProcessProvider
    {
        //private static UI_DefaultControlPermissionProcessProvider _default = new UI_DefaultControlPermissionProcessProvider();

        public UI_DefaultControlPermissionProcessProvider()
        {
            ProcessTypes=new[]{typeof(Control)};
        }

        public IEnumerable<Type> ProcessTypes { get; private set; }

        public bool SetControlStatus(object obj, UI_PermissionFaildHandle status)
        {
            if (obj == null || !(obj is Control))
            {
                return false;
            }

            var ctrl = (Control)obj;

            switch (status)
            {
                case UI_PermissionFaildHandle.Disable:
                    if (ctrl is TextBox)
                    {
                        ((TextBox)ctrl).ReadOnly = true;
                    }
                    else
                    {
                        ctrl.Enabled = false;
                    }
                    break;
                case UI_PermissionFaildHandle.Hide:
                    ctrl.Visible = false;
                    break;
            }

            return true;
        }

        public bool SetControlStatus(IEnumerable<object> ctrlList, UI_PermissionFaildHandle status)
        {
            if (ctrlList == null)
            {
                return false;
            }

            foreach (var c in ctrlList)
            {
                SetControlStatus(c, status);
            }

            return true;
        }

        public bool ResetControlStatus(object obj)
        {
            if (obj == null)
            {
                return true;
            }

            var ctrl = (Control)obj;

            if (ctrl is TextBox)
            {
                var txt = (TextBox)ctrl;

                txt.ReadOnly = false;
                txt.Enabled = true;
                txt.Visible = true;
            }
            else
            {
                ctrl.Enabled = true;
                ctrl.Visible = true;
            }

            return true;
        }

        public bool ResetControlStatus(IEnumerable<object> ctrlList)
        {
            if (ctrlList == null)
            {
                return true;
            }

            foreach (var c in ctrlList)
            {
                ResetControlStatus(c);
            }

            return true;
        }

        public IEnumerable<object> GetChildControls(object parentObj)
        {
            if (parentObj == null || !(parentObj is Control))
            {
                yield break;
            }

            var cl = ((Control)parentObj).Controls;

            if (cl == null || cl.Count <= 0)
            {
                yield break;
            }

            foreach (var c in cl)
            {
                yield return c;
            }

        }

        //public static IUI_ControlPermissionProcessProvider Default
        //{
        //    get { return _default; }
        //}

        #region IUI_ControlPermissionProcessProvider 成员


        public string GetControlID(object ctrl)
        {
            if (ctrl is Control)
            {
                return (ctrl as Control).Name.ToStringEx();
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion
    }
}